<%--
  Created by IntelliJ IDEA.
  User: cdyoue
  Date: 2018/11/19
  Time: 17:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>

</head>
<style>
    .formWrap {
        width: 500px;
        margin: 50px auto 0;
        text-align: center;
        border: 1px solid #999;
        padding: 20px;

    }

    .formWrap table {
        margin: 0 auto 20px;
    }
    .dd{
        background-image: url("image/bk.jpg");
    }
</style>
<body class="dd">
<%@ include file="head.jsp" %>
<div class="formWrap">
    <h1 id="title">登录</h1>
    <table>
        <tr>
            <td>用户名称</td>
            <td>
                <input id="username" type="text" value="1234">
            </td>
        </tr>

        <tr>
            <td>用户密码</td>
            <td>
                <input id="password" type="password" value="1234">
            </td>
        </tr>
        <tr>
            <td>
                <button id="login">登录</button>
            </td>
            <td>
                <button id="register">注册</button>
                <button style="display: none" id="save">提交</button>
            </td>
        </tr>
    </table>
</div>

</body>
<script>
    var basePath = $("#basePath").attr("path");
    console.info(basePath)
    $("#login").click(function () {
        var username = $("#username").val();
        var password = $("#password").val();
        if (!username) {
            alert("用户名不能为空");
            return
        }

        if (!password) {
            alert("密码不能为空");
            return
        }

        var data = {};
        data.username = username;
        data.password = password;
        $.post(basePath + "system/login", data, function (rep) {
            var code = rep.code
            if (code == 20) {
                window.location.href = basePath + "index";
                window.reload();
            } else {
                alert(rep.message);
            }
        });
    });

    $("#save").click(function () {
        var username = $("#username").val();
        var password = $("#password").val();
        if (!username) {
            alert("用户名不能为空");
            return
        }

        if (!password) {
            alert("密码不能为空");
            return
        }

        var data = {};
        data.username = username;
        data.password = password;

        $.ajax({
            url: basePath + 'system/regist',
            type: 'post',
            data: data,
            success: function (res) {
                if (res.code === 20) {
                    window.location.href = basePath + "index";
                } else {
                    alert(res.message);
                }
            }
        })


    });

    $("#register").click(function () {
        $("#login").hide();
        $("#save").show();
        $("#title").html("注册");
        $(this).hide();
    })

</script>
</html>
